package dmcs.inventorysystem.view.beans;


import dmcs.inventorysystem.view.utils.ADFUtils;

import javax.faces.event.ActionEvent;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.share.logging.ADFLogger;
import oracle.adf.view.rich.component.rich.data.RichTable;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import org.apache.myfaces.trinidad.event.SelectionEvent;


public class SearchBean {

    private static final ADFLogger LOG = ADFLogger.createADFLogger(SearchBean.class);

    public SearchBean() {
    }
    
    public void editInventoryEntry(SelectionEvent selectionEvent) {
        LOG.entering(getClass().getName(), "editInventoryEntry");
        RichTable table = (RichTable)selectionEvent.getSource();
        ADFUtils.editEntry(table, "setCurrentRowWithKeyValue", "Edit");
        LOG.exiting(getClass().getName(), "editInventoryEntry");
    }

    public void editPersonEntry(SelectionEvent selectionEvent) {
        LOG.entering(getClass().getName(), "editPersonEntry");
        RichTable table = (RichTable)selectionEvent.getSource();
        ADFUtils.editEntry(table, "setCurrentRowWithKey", "Edit");
        LOG.exiting(getClass().getName(), "editPersonEntry");
    }

    private BindingContainer getBindings() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    public void createInsertEntry(ActionEvent actionEvent) {
        actionEvent.toString();
        BindingContainer bindings = getBindings();
        OperationBinding createInventoryEntry = bindings.getOperationBinding("CreateInsertInventoryEntry");
        createInventoryEntry.execute();
    }

    public String createInvoice() {
        LOG.entering(getClass().getName(), "createInvoice");
        BindingContainer bindings = getBindings();
        DCIteratorBinding inventories = (DCIteratorBinding)bindings.get("InventoriesView1Iterator");
        inventories.getCurrentRow();

        OperationBinding operationBinding = bindings.getOperationBinding("CreateInsert");
        operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            return null;
        }
        LOG.exiting(getClass().getName(), "createInvoice");
        return "Create";
    }

    public String createBuilding() {
        BindingContainer bindings = getBindings();
        DCIteratorBinding inventories = (DCIteratorBinding)bindings.get("InventoriesView1Iterator");
        inventories.getCurrentRow();
        
        OperationBinding operationBinding = bindings.getOperationBinding("CreateInsert");
        operationBinding.execute();
        if (!operationBinding.getErrors().isEmpty()) {
            return null;
        }
        return "CreateOrEdit";
    }
}
